<?php

/**
 * Description of member_model
 *
 * @author dian
 */
class Member_Model extends MY_Model{
    
    function __construct() {
        parent::__construct();
    }
    
    public function login($nohp, $pin){
        $this->db->from('member');
        $this->db->where(array(
            "no_hp" => $nohp,
            "pin"   => strtoupper(md5($pin))
        ));
        
        return $this->db->count_all_results() > 0;
    }
    
    public function daftar($data = array()){
        $nohp = isset($data["no_hp_registran"]) ? $data["no_hp_registran"] : "0";
        
        $this->db->from('member');
        $this->db->where('no_hp', $nohp);
        if($this->db->count_all_results() > 0){
            return -1;
        }
        
        $this->db->from('request_daftar');
        $this->db->where('no_hp_registran', $nohp);
        if($this->db->count_all_results() > 0){
            return -1;
        }
        
        return $this->db->insert("request_daftar", $data);
    }
    
    public function get_alias_by_hp($nohp){
        $this->db->select("alias_member");
        $this->db->from('member');
        $this->db->where('no_hp', $nohp);
        $this->db->limit(1);
        
        $row = $this->db->get()->row();
        
        return isset($row->alias_member) ? $row->alias_member : "";
    }
    
    public function get_data_by_alias($alias){
        $this->db->select("*");
        $this->db->from('member');
        $this->db->where('alias_member', $alias);
        
        return $this->db->get()->row();
    }
    
    public function get_id_by_alias($alias){
        $row = $this->get_data_by_alias($alias);
        
        return isset($row->id_member) ? $row->id_member : "0";
    }
    
    public function get_data_by_id($id){
        $this->db->select("*");
        $this->db->from('member');
        $this->db->where('id_member', $id);
        
        return $this->db->get()->row();
    }
    
    public function get_downline_not_yet_activated($alias){
        $this->db->select("*");
        $this->db->from('request_daftar');
        $this->db->where(array('alias_member' => $alias, "is_approved" => "0", 
                               "date_activated" => "0000-00-00 00:00:00"));
        $this->db->where("id_request_pin <> '' AND id_request_pin <> 0", null, false);
        
        return $this->db->get();
    }
    
    public function update_data($alias, $data_update){
        $this->db->where('alias_member', $alias);
        $this->db->update("member", $data_update);
    }
    
    public function is_activation_token_valid($alias, $id_req_daftar, $id_req_pin){
        $this->db->select("id_request_pin");
        $this->db->from('request_daftar');
        $this->db->where(array('alias_member' => $alias, "is_rejected" => "0", "id_request" => $id_req_daftar));
        
        $row = $this->db->get()->row();
        if(isset($row->id_request_pin) && $row->id_request_pin != ""){
            $this->db->select("id_request");
            $this->db->from('request_pin');
            $this->db->where(array('alias_member' => $alias, "id_request" => $id_req_pin));
            $this->db->where("key_token is not null AND key_token <> '' AND is_avail is null", null, false);

            $row2 = $this->db->get()->row();
            
            if(isset($row2->id_request) && $row->id_request_pin == $row2->id_request){
                return true;
            }
        }
        
        return false;
    }
    
    public function update_activation($id_request){
        $this->db->where('id_request', $id_request);
        $this->db->update("request_daftar", [
            "date_activated" => date("Y-m-d H:i:s")
        ]);
    }
    
    public function increment_complaint_response($id_komplain){
        $this->db->where('id_komplain', $id_komplain);
        $this->db->set('jumlah_respon', 'jumlah_respon + 1', FALSE);
        $this->db->update('komplain');
    }
    
}
